package com.guicent.geekai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guicent.geekai.entity.User;
import com.guicent.geekai.mapper.UserMapper;
import com.guicent.geekai.service.UserService;
import com.guicent.geekai.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean checkLogin(String account, String password) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account", account);
        User user = userMapper.selectOne(queryWrapper);
        return user != null && user.getPassword().equals(MD5Util.encrypt(password));
    }

    @Override
    public boolean existsByAccount(String account) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account", account);
        return userMapper.selectCount(queryWrapper) > 0;
    }

    @Override
    public void saveUser(User newUser) {
        try {
            userMapper.insert(newUser);
        }catch (Exception e){
            throw new RuntimeException("注册失败");
        }
    }
}